﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.IO;

namespace FlyBird
{
    class GameBarrierLayer
    {
        //游戏进行时间，用于记录
        private GameTime gameTime;

        //从txt中读取的记录
        private int txt;
        
        //生成随机数
        private Random random = new Random();

        //存放障碍物的容器
        public List<Barrier> barriers;

        //构造函数
        public GameBarrierLayer()
        {
            barriers = new List<Barrier>();
            gameTime = new GameTime();
        }

        //绘制障碍物
        public void draw(Graphics g, Bird bird)
        {
            for (int i = 0; i < barriers.Count; i++)
            {
                Barrier barrier = barriers[i];
                
                //当前障碍物还可见
                if (barrier._visible)
                {
                    barrier.draw(g);
                }
                //超出屏幕，移除
                else
                { 
                    Barrier remove = barriers[i];
                    barriers.RemoveAt(i);
                    Barrierpool.setPool(remove);
                    i--;
                }
            }
            //小鸟碰撞
            collideBird(bird);
            //控制障碍物
            logic(g);
        }

        //随机生成障碍物
        public void logic(Graphics g)
        {
            //障碍物为0
            if (barriers.Count == 0)
            {
                ran();
                gameTime.begin();
                //Barrier top = new Barrier(600, 0, numberTop, Barrier.TYPE_TOP_NORMAL);
                //barriers.Add(top);
                //Barrier down = new Barrier(600, 500 - numberDown, numberDown, Barrier.TYPE_BOTTOM_NORMAL);
                //barriers.Add(down);
                insert(600, 0, numberTop, Barrier.TYPE_TOP_NORMAL);
                insert(600, 500 - numberDown, numberDown, Barrier.TYPE_BOTTOM_NORMAL);
            }
            else
            {
                g.DrawImage(StaticValue.GAME_SCORE, 0, 0, 100, 75);
                long differ = gameTime.getDiffer();
                Font font = new Font("微软雅黑", 10, FontStyle.Bold);
                Brush brush = new SolidBrush(Color.White); // 使用 SolidBrush 创建 Brush 对象                    
                g.DrawString(differ.ToString(), font, brush, 15, 40);
                
                //判断是否破记录
                txt = getTxt();
                if (differ <= txt)
                {
                    g.DrawString(txt.ToString(), font, brush, 60, 40);
                }
                else
                {
                    setTxt(differ.ToString());
                    g.DrawString(getTxt().ToString(), font, brush, 60, 40);
                }
                
                //判断最后一个障碍物是否完全进入屏幕
                Barrier last = barriers[barriers.Count - 1];
                if (last.isInFrame())
                {
                    ran();
                    //Barrier top = new Barrier(600, 0, numberTop, Barrier.TYPE_TOP_NORMAL);
                    //barriers.Add(top);
                    //Barrier down = new Barrier(600, 500 - numberDown, numberDown, Barrier.TYPE_BOTTOM_NORMAL);
                    //barriers.Add(down);
                    if (number < 75)
                    {
                        //绘制悬浮障碍物
                        //随机高度
                        int randomHeight = random.Next(250) + 50;
                        //随机位置
                        int randomY = random.Next(50) + 30;
                        //悬浮障碍物
                        insert(600, 32, randomHeight, Barrier.TYPE_HOVER_NORMAL);
                    }
                    else if (number > 425)
                    {
                        //绘制可移动悬浮障碍物
                        //随机高度
                        int randomHeight = random.Next(150) + 50;
                        //随机位置
                        int randomY = random.Next(50) + 100;
                        //可移动
                        insert(600, randomHeight, randomY, Barrier.TYPE_MOVE_HOVER_NORMAL);
                    }
                    else if (number < 300)
                    {
                        //上下障碍
                        insert(600, 0, numberTop, Barrier.TYPE_TOP_NORMAL);
                        insert(600, 500 - numberDown, numberDown, Barrier.TYPE_BOTTOM_NORMAL);
                    }
                    else
                    {
                        //上下可移动障碍
                        int randomType = random.Next(3);
                        if (randomType == 1)
                        { 
                            //上部移动
                            insert(600, 0, numberTop, Barrier.TYPE_MOVE_TOP_NORMAL);
                            insert(600, 500 - numberDown, numberDown, Barrier.TYPE_BOTTOM_NORMAL);
                        }
                        else if (randomType == 2)
                        {
                            //下部移动
                            insert(600, 0, numberTop, Barrier.TYPE_TOP_NORMAL);
                            insert(600, 500 - numberDown, numberDown, Barrier.TYPE_MOVE_BOTTOM_NORMAL);
                        }
                        else
                        { 
                            //上下均移动
                            insert(600, 0, numberTop, Barrier.TYPE_MOVE_TOP_NORMAL);
                            insert(600, 500 - numberDown, numberDown, Barrier.TYPE_MOVE_BOTTOM_NORMAL);
                        }
                    }
                }
            }
        }

        //文件读写，用于存放最高记录
        string file = "game.txt";
        public int getTxt()
        {
            int read;
            try
            {
                string filePath = file;
                using (StreamReader sr = new StreamReader(filePath))
                {
                    string line = sr.ReadLine();
                    read = int.Parse(line);
                }
            }
            catch (IOException e)
            {
                Console.WriteLine("Error: " + e.Message);
                read = 0;
            }
            return read;
        }

        public void setTxt(string str)
        {
            string filePath = "game.txt";
            try
            {
                using (StreamWriter sw = new StreamWriter(filePath))
                {
                    sw.WriteLine(str);
                }
            }
            catch (IOException e)
            {
                Console.WriteLine("Error: " + e.Message);
            }
        }

        
        //从线程池获取对象，并把参数封装成Barrier存到barriers中
        public void insert(int x, int y, int height, int type)
        {
            //获取最新的障碍物
            Barrier top = Barrierpool.getPool();
            top._x = x;
            top._y = y;
            top._height = height;
            top._initHeight = height;
            top._type = type;
            top._visible = true;
            barriers.Add(top);
        }

        //上方障碍物的高度
        private int numberTop;
        //下方障碍物的高度
        private int numberDown;
        //随机生成悬浮障碍物,上障碍，下障碍，移动
        private int number;
        //生成两个100-500之间的随机高度
        public void ran()
        {
            numberTop = random.Next(400) + 100;
            numberDown = random.Next(400) + 100;
            number = random.Next(500);
            //重合便重新生成
            if (numberTop + numberDown > 400)
            {
                ran();
            }
        }
        //判断小鸟与障碍物是否发生碰撞
        public bool collideBird(Bird bird)
        {
            for (int i = 0; i < barriers.Count; i++)
            { 
                Barrier barrier = barriers[i];
                //判断是否相交
                if (barrier._rect.IntersectsWith(bird._rect))
                {
                    bird.life = false;
                }
            }
            return false;
        }
        //清空障碍物的池子
        public void restant()
        {
            barriers.Clear();
        }
    }
}
